var chatObjs = {};
var username = jQuery("#chat_user").val();

/**
Function that updates chat

Params:
	username - username of user
	chatRoom - chatRoom id of the chatRoom user is connected to
*/
/*var updateChat = function(username, chatID, lastUpdated){
	jQuery.ajax({
            type:'POST',
            url: './process/processGetChat.php',
            data: "username=" + username + "&chat_room=" + chatID + "&last_updated=" + lastUpdated,
            success:function(data){
					console.log(data);
					var splitArray = data.split(", ");
					var lastUpdated = splitArray[splitArray.length];
					var i = 0;
					data = "";
					while(i < (splitArray.length - 1)){
						data = data + ", " + splitArray[i];
						i++;
					}
					if(data != jQuery("#chat_form_" + chatID + " .chat_text").val()){
						jQuery("#chat_window_" + chatID + " .chat_text").val(data);
						jQuery("#chat_window_" + chatID + " .chat_text").get(0).scrollTop = jQuery("#chat_window_" + chatID + " .chat_text").get(0).scrollHeight;
					}
			}
    });
}*/

/**
New function to pull all chatrooms user is a part of
*/
var getChatRooms = function(username){
	jQuery.ajax({
            type:'POST',
            url: './process/processGetActiveChats.php',
            data: "username=" + username,
            success:function(response){
					var activeChats = response.replace("{","").replace("}","").split(", ");
					activeChats.forEach(function(chatID){
						if(chatObjs[chatID] == null){
							chatObjs[chatID] = new chat(chatID, username, 0);
							var htmlToAdd = chatObjs[chatID].getHTML();
							jQuery("body").append(htmlToAdd);
							jQuery("#chat_form_" + chatID + " .chat_input").keyup(function(e){
								if(e.keyCode == 13){
									jQuery("#chat_form_" + chatID + " .send_button").trigger("click");
								}	
							});
						}
						var lastUpdated = jQuery("#chat_form_" + chatID + " .chat_update_time").val();
						jQuery.ajax({
							type:'POST',
							url: './process/processGetChat.php',
							data: "username=" + username + "&chat_room=" + chatID + "&last_updated=" + lastUpdated,
							success:function(data){
								console.log("return");
								var splitArray = data.split(", ");
								var lastUpdated = splitArray[splitArray.length];
								var i = 0;
								data = "";
								while(i < (splitArray.length - 1)){
									data = data + ", " + splitArray[i];
									i++;
								}
								if(data != jQuery("#chat_form_" + chatID + " .chat_text").val()){
									jQuery("#chat_window_" + chatID + " .chat_text").val(data);
									jQuery("#chat_window_" + chatID + " .chat_text").get(0).scrollTop = jQuery("#chat_window_" + chatID + " .chat_text").get(0).scrollHeight;
								}
								// Recursively called function to repeat after successful response has been sent from ajax call.
								setTimeout(function(){
											getChatRooms(username);
								}, 1000);
							}
						});
					});
			}
    });
}

jQuery("document").ready(function(){
	getChatRooms(username);
});

/**
Function that updates chat text box every second

window.setInterval(function(){
	var username = jQuery("#chat_user").val();
	getChatRooms(username);
}, 1000);
*/